import tkinter as tk
from tkinter import ttk

class ColumnSelector:
    def __init__(self, parent, columns, column_types, title, callback):
        self.callback = callback
        self.selected_columns = []

        self.window = tk.Toplevel(parent)
        self.window.title(title)
        self.window.geometry('300x400')

        # 创建说明标签
        ttk.Label(self.window, text="选择需要处理的列：").pack(pady=10)

        # 创建列表框和滚动条
        frame = ttk.Frame(self.window)
        frame.pack(fill=tk.BOTH, expand=True, padx=10)

        scrollbar = ttk.Scrollbar(frame)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

        self.listbox = tk.Listbox(frame, selectmode=tk.MULTIPLE, yscrollcommand=scrollbar.set)
        self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        scrollbar.config(command=self.listbox.yview)

        # 添加列名到列表框
        for col in columns:
            col_type = column_types[col]
            self.listbox.insert(tk.END, f"{col} ({col_type})")

        # 创建按钮框架
        button_frame = ttk.Frame(self.window)
        button_frame.pack(fill=tk.X, padx=10, pady=10)

        ttk.Button(button_frame, text="确定", command=self.on_confirm).pack(side=tk.LEFT, padx=10)
        ttk.Button(button_frame, text="取消", command=self.window.destroy).pack(side=tk.LEFT)

    def on_confirm(self):
        # 获取选中的列名（去除类型信息）
        self.selected_columns = [self.listbox.get(i).split(' (')[0] 
                               for i in self.listbox.curselection()]
        self.callback(self.selected_columns)
        self.window.destroy()

class ParameterDialog:
    def __init__(self, parent, params, title, callback):
        self.callback = callback
        self.params = params
        self.values = {}

        self.window = tk.Toplevel(parent)
        self.window.title(title)
        self.window.geometry('400x300')

        # 创建主框架
        main_frame = ttk.Frame(self.window)
        main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

        # 创建参数输入区域
        for param_name, param_info in params.items():
            frame = ttk.Frame(main_frame)
            frame.pack(fill=tk.X, pady=5)

            ttk.Label(frame, text=param_info['label']).pack(side=tk.LEFT)

            if param_info['type'] == 'choice':
                var = tk.StringVar(value=param_info['default'])
                combo = ttk.Combobox(frame, textvariable=var, values=param_info['choices'])
                combo.pack(side=tk.RIGHT, fill=tk.X, expand=True)
                self.values[param_name] = var

            elif param_info['type'] == 'int':
                var = tk.IntVar(value=param_info['default'])
                spin = ttk.Spinbox(frame, from_=param_info['min'], to=param_info['max'],
                                  textvariable=var)
                spin.pack(side=tk.RIGHT, fill=tk.X, expand=True)
                self.values[param_name] = var

            elif param_info['type'] == 'str':
                var = tk.StringVar(value=param_info['default'])
                entry = ttk.Entry(frame, textvariable=var)
                entry.pack(side=tk.RIGHT, fill=tk.X, expand=True)
                self.values[param_name] = var

        # 创建按钮区域
        button_frame = ttk.Frame(self.window)
        button_frame.pack(fill=tk.X, padx=10, pady=10)

        ttk.Button(button_frame, text="确定", command=self.on_confirm).pack(side=tk.LEFT, padx=10)
        ttk.Button(button_frame, text="取消", command=self.window.destroy).pack(side=tk.LEFT)

    def on_confirm(self):
        result = {param_name: var.get() for param_name, var in self.values.items()}
        self.callback(result)
        self.window.destroy()